package com.monas.client.jfree;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.jfree.experimental.chart.swt.ChartComposite;

public class MonasPieChartComposite {

	private final String title;

	private Composite panel;

	private final String[] headers;

	private MonasPieChart chart;

	private ChartComposite frame;

	public MonasPieChartComposite(Composite parent, String title,
			String... headers) {

		this.title = title;
		this.headers = headers;
		/*
		 * Buat panel
		 */
		panel = new Composite(parent, SWT.BORDER);

		/*
		 * Siapkan layout untuk mengatur panel
		 */
		panel.setLayoutData(new GridData(GridData.FILL_BOTH));
		panel.setLayout(new FillLayout());
	}

	public void createChart(String... values) {
		chart = new MonasPieChart(title);

		addValues(values);

		chart.createChart();
		createFrameChart();
	}

	private void createFrameChart() {
		frame = new ChartComposite(panel, SWT.NONE, chart.getChart(), true);
		frame.setDisplayToolTips(true);
		frame.setHorizontalAxisTrace(false);
		frame.setVerticalAxisTrace(false);
	}

	public void refresh(String title, String[] fields) {
		chart.clearData();
		chart.setTitle(title);

		addValues(fields);

		frame.layout();
	}

	private void addValues(String[] values) {
		for (int i = 0; i < values.length; i++) {
			chart.addValue(headers[i], Double.parseDouble(values[i]));
		}
	}

	public Control getComposite() {
		return panel;
	}
	
	public MonasPieChart getChart() {
		return chart;
	}

}
